Blink an external LED


Attach an LED, get it to flash

In the previous recipe, we got the Arduino's on-board LED to flash. In this recipe, we attach an external LED and get it to flash.

This illustrates the key concept of driving digital outputs on the Arduino.

Build the Circuit

You will need:

Wire up the circuit as follows:

Enter the Code

Copy the code below and overwrite the code in the Arduino IDE.

// Constants
const int LEDPIN = 9;

// Setup is run once
void setup() {
  // Tell Arduino which pins are input and output
  pinMode(LEDPIN, OUTPUT);
}

// Loop is run over and over again
void loop() {
  // Flash the LED
  digitalWrite(LEDPIN,HIGH);
  delay(1000);
  digitalWrite(LEDPIN,LOW);
  delay(1000);
}

Run the Code

Now upload the code to the Arduino.

The LED should start flashing.

Try to move the LED to pin 10 and get it to flash. You will need to move the physical wire connected to the Arduino and make a change in the code.

How it Works

This code is very similar to the code we used for the built-in LED sketch, but in this case we have defined the specific Arduino pin we have attached the LED to (pin 9).

The on-board LED is linked to pin 13, so if you attach an LED to pin 13 and change the value of the LEDPIN constant to 13, both the external LED and the on-boad LED will flash.